//
//  MqMineCollectViewController.m
//  Meiqi
//
//  Created by lanou on 16/1/21.
//  Copyright © 2016年 KFC. All rights reserved.
//

#import "MqMineCollectViewController.h"
#import "MqInfoSearchTableViewCell.h"
#import "MqMyCollectionModel.h"
#import "MqInfoWebViewController.h"
@interface MqMineCollectViewController ()
<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) NSMutableArray *dataArray;

@end

@implementation MqMineCollectViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationController.navigationBar.hidden = NO;
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"收藏";
    
    //  编辑
//    UIBarButtonItem *editItem = [[UIBarButtonItem alloc] initWithTitle:@"编辑" style:(UIBarButtonItemStylePlain) target:self action:@selector(editItem:)];
    
//    self.navigationItem.rightBarButtonItem = editItem;
    
    //  初始化
    _dataArray = [NSMutableArray array];
    
    //  数据处理
    [self dataProcess];
    
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight) style:(UITableViewStylePlain)];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.rowHeight = 80;
    [self.view addSubview:_tableView];
    
    [_tableView registerClass:[MqInfoSearchTableViewCell class] forCellReuseIdentifier:@"MqInfoSearchTableViewCell"];
  
}

- (void)dataProcess{
    
    // 建表
    [[MqDatabase dataBaseHandle] buildListWithString:@"CREATE TABLE IF NOT EXISTS collection (account text, smalltext text, title text, newstime text, idsURL text,authorInfoURL text,tagListURL text,recommendListURL text, commentURL text)"];
    
    NSString *account = [[NSUserDefaults standardUserDefaults] objectForKey:@"account"];
    
    FMResultSet *result = [[MqDatabase dataBaseHandle].db executeQuery:[NSString stringWithFormat:@"SELECT * FROM collection where account = %@",account]];
    while ([result next]) {
        
        NSString *account = [result stringForColumn:@"account"];
        //  取出数据库中每一行字段的值
        NSString *smalltext = [result stringForColumn:@"smalltext"];
        
        NSString *title = [result stringForColumn:@"title"];
        
        NSString *newstime = [result stringForColumn:@"newstime"];
        
        NSString *idsURL = [result stringForColumn:@"idsURL"];
        
        NSString *authorInfoURL = [result stringForColumn:@"authorInfoURL"];
        NSString *tagListURL = [result stringForColumn:@"tagListURL"];
        NSString *recommendListURL = [result stringForColumn:@"recommendListURL"];
        
        NSString *commentURL = [result stringForColumn:@"commentURL"];
        
        MqMyCollectionModel *model = [[MqMyCollectionModel alloc] init];
        
        model.account = account;
        model.smalltext = smalltext;
        model.title = title;
        model.newstime = newstime;
        model.idsURL = idsURL;
        model.authorInfoURL = authorInfoURL;
        model.tagListURL = tagListURL;
        model.recommendListURL = recommendListURL;
        model.commentURL = commentURL;
        
        [_dataArray addObject:model];
        
    }
}

//  允许编辑
- (void)editItem:(UIBarButtonItem *)barItem{
    
    //  更改tableView的编辑状态
    [self setEditing:!self.editing animated:YES];
    
    if (self.editing == YES) {
        
        barItem.title = @"完成";
        
    }else{
        
        barItem.title = @"编辑";
        
    }
    
}
//  可以编辑
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return YES;
}


//  编辑样式
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return UITableViewCellEditingStyleDelete;
    
}

//  完成编辑
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    
    MqMyCollectionModel *model = _dataArray[indexPath.row];
    
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        
        NSString *str = [NSString stringWithFormat:@"DELETE FROM collection where account = %@ and smalltext = \'%@\'",model.account, model.smalltext];
        
        [[MqDatabase dataBaseHandle].db executeUpdate:str];
        
        [self.dataArray removeObjectAtIndex:indexPath.row];
        
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:(UITableViewRowAnimationLeft)];
    }
    
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    MqInfoSearchTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MqInfoSearchTableViewCell" forIndexPath:indexPath];
    
    MqMyCollectionModel *model = _dataArray[indexPath.row];
    cell.titleLabel.text = model.title;
    cell.smallTextLabel.text = model.smalltext;
    cell.timeLabel.text = model.newstime;
    
    
    return cell;
    
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return _dataArray.count;
    
}

//  cell点击方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    MqMyCollectionModel *model = _dataArray[indexPath.row];
    
    MqInfoWebViewController *webView = [[MqInfoWebViewController alloc] init];
    webView.idsURL = model.idsURL;
    
    // 标签的网址
    webView.tagListURL = model.tagListURL;
    
    // 作者信息的网址
    webView.authorInfoURL = model.authorInfoURL;
    
    // 相关文章的网址
    webView.recommendListURL = model.recommendListURL;    // 评论的网址
    webView.commentURL = model.commentURL;
    
    [self.navigationController pushViewController:webView animated:YES];
    
}

- (void)viewWillDisappear:(BOOL)animated{
    
    self.navigationController.navigationBar.hidden = YES;
    
}

- (void)viewWillAppear:(BOOL)animated{
    
    self.navigationController.navigationBar.hidden = NO;

}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
